package com.css.openapi.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.css.common.core.constant.AppCons;
import com.css.common.core.enums.FxOrderStatusEnum;
import com.css.common.core.enums.PayKpEnum;
import com.css.common.core.exception.ServiceException;
import com.css.common.core.utils.IdempotentUtil;
import com.css.openapi.BO.*;
import com.css.openapi.BO.fxdw.*;
import com.css.openapi.entity.ZfjkpFxkpDdxxbDO;
import com.css.openapi.feign.ZfjkpPtCommonClient;
import com.css.openapi.service.FxdwService;
import com.css.openapi.service.ZfjkpFxkpDdxxbService;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @description:
 * @Title: FxdwServiceImpl
 * @Author xlw
 * @Package com.invoice.tcc.service.impl
 * @Date 2024/11/19 17:29
 */
@Setter(onMethod_ = @Resource)
@Service
@Slf4j
public class FxdwServiceImpl implements FxdwService {


    private ZfjkpFxkpDdxxbService zfjkpFxkpDdxxbService;

    @Resource
    private ZfjkpPtCommonClient zfjkpPtCommonClient;

    /**
     * 农产品订单审核
     *
     * @param params 参数
     * @return {@link Boolean }
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean ncpOrderAudit(NcpOrderAuditBO params, boolean isSystemPay) {
        //幂等校验
        IdempotentUtil.verify(StrUtil.concat(true, params.getDdbh(), params.getYwlsh()), IdempotentUtil.IDEMPOTENT_TIME);

        SimpleBooleanResponse response =  zfjkpPtCommonClient.ncpOrderAudit(params);
        if(!Objects.equals(response.getCode(), "0"))throw new ServiceException(response.getMsg());
        if(response.getData()){
            //校验商户编号
            ZfjkpFxkpDdxxbDO order = zfjkpFxkpDdxxbService.lambdaQuery()
                    .eq(StrUtil.isNotBlank(params.getDdbh()), ZfjkpFxkpDdxxbDO::getDdbh, params.getDdbh())
                    .eq(StrUtil.isNotBlank(params.getYwlsh()), ZfjkpFxkpDdxxbDO::getYwlsh, params.getYwlsh())
                    .eq(ZfjkpFxkpDdxxbDO::getShbdbh, params.getShbdbh())
                    .one();
            if (Objects.isNull(order)) {
                throw new ServiceException("订单不存在");
            }
            if (!Objects.equals(FxOrderStatusEnum.WAIT_AUDIT.getCode(), order.getDdzt())) {
                throw new ServiceException("订单已审核");
            }

            if (Objects.equals(params.getShbz(), AppCons.NCP_ORDER_AUDIT_NO_PASS)) {
                //审核不通过
                order.setDdzt(FxOrderStatusEnum.AUDIT_FAIL.getCode());
                return zfjkpFxkpDdxxbService.updateById(order);
            }
            //审核通过
            PayKpEnum payKpEnum = PayKpEnum.getInstance(order.getZfkpqrfs());
            String ddzt = FxOrderStatusEnum.convert(payKpEnum, isSystemPay).getCode();
            order.setDdzt(ddzt);
            zfjkpFxkpDdxxbService.updateById(order);
        }
        return response.getData();
    }

    @Override
    public FxkpZszyOrderBO createZszyOrder(FxkpOrderBO fxkpOrderBO) {
        //幂等校验
        IdempotentUtil.verify(fxkpOrderBO.getYwlsh(), IdempotentUtil.IDEMPOTENT_TIME);
        CreateZszyOrderResponse response =  zfjkpPtCommonClient.createZszyOrder(fxkpOrderBO);
        if(!Objects.equals(response.getCode(), "0"))throw new ServiceException(response.getMsg());
        //todo 保存数据
        FxkpZszyOrderBO fxkpZszyOrderBO = new FxkpZszyOrderBO();
        BeanUtil.copyProperties(response, fxkpZszyOrderBO);
        return fxkpZszyOrderBO;
    }

    @Override
    public ZszyOrderStatusBO queryZszyOrderStatus(FxOrderStatusReq req) {
        QueryZszyOrderStatusResponse response =  zfjkpPtCommonClient.queryZszyOrderStatus(req);
        if(!Objects.equals(response.getCode(), "0"))throw new ServiceException(response.getMsg());
        //todo 保存数据
        ZszyOrderStatusBO zszyOrderStatusBO = new ZszyOrderStatusBO();
        BeanUtil.copyProperties(response, zszyOrderStatusBO);
        return zszyOrderStatusBO;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ZszyOrderPayResponseBO zszyOrderPay(ZszyPayBO params) {
        //幂等校验
        IdempotentUtil.verify(StrUtil.concat(true, params.getDdbh(), params.getYwlsh()), IdempotentUtil.IDEMPOTENT_TIME);
        ZszyOrderPayResponse response =  zfjkpPtCommonClient.zszyOrderPay(params);
        if(!Objects.equals(response.getCode(), "0"))throw new ServiceException(response.getMsg());
        //todo 保存数据
        ZszyOrderPayResponseBO zszyOrderPayResponseBO = new ZszyOrderPayResponseBO();
        BeanUtil.copyProperties(response, zszyOrderPayResponseBO);
        return zszyOrderPayResponseBO;
    }

    @Override
    public Map<String, Object> downloadelecreceipt(DownloadElecreceiptBO params) {
        DownloadElecreceiptResponse response =  zfjkpPtCommonClient.downloadelecreceipt(params);
        if(!Objects.equals(response.getCode(), "0"))throw new ServiceException(response.getMsg());
        Map<String, Object> map = new HashMap<>();
        map.put("dzhdImg",response.getDzhdImg());
        return map;
    }
}
